package structure;

public class Test8 {
    public static void main(String[] args) {

    }
    //leetcode 26 删除重复项
    public static int removeDuplicates(int[] nums) {
        int num = 1;
        int i = 0;
        int j = 1;
        for (i = 0; i < nums.length - 1; i++) {
            if(nums[i] != nums[i + 1]){
                nums[j] = nums[i + 1];
                num++;
                j++;
            }
        }
        return num;
    }
    //leetcode 1816 截断句子
    public String truncateSentence(String s, int k) {
        int len = s.length();
        int index = 0;
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if (c == ' ') {
                k--;
            }
            if (k == 0) {
                index = i;
                break;
            }
        }
        if (k == 0) {
            return s.substring(0, index);
        } else {
            return s;
        }
    }
    //牛客 求平方根
    public int mysqrt (int x) {
        if (x == 0 || x == 1) {
            return x;
        }
        int num = 0;
        int i = 1;
        while (true) {
            if (i * i <= x && (i + 1) * (i + 1) >= x) {
                if ((i + 1) * (i + 1) > x) {
                    num = i;
                } else {
                    num = i + 1;
                }
                break;
            }
            i++;
        }
        return num;
    }
    //牛客 回文字符串
    public static boolean judge (String str) {
        // write code here
        int len = str.length();
        String left = str.substring(0,len >> 1);
        StringBuilder Right;
        if(len % 2 == 0) {
            Right = new StringBuilder(str.substring((len >> 1), len));
        }else {
            Right = new StringBuilder(str.substring((len >> 1) + 1,len));
        }
        Right = Right.reverse();
        String right = Right.toString();
        if(left.equals(right)){
            return true;
        }
        return false;
    }
}
